<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item prop="keyName">
        <el-input v-model="queryParams.keyName" placeholder="字段/字段内容" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="技术环节" prop="fieldCategory">
        <el-select v-model="queryParams.fieldCategory" placeholder="全部" clearable style="width: 240px">
          <el-option v-for="item in fieldCategoryType" :key="item.type" :label="item.title" :value="item.type" />
        </el-select>
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="全部" clearable style="width: 240px">
          <el-option value="" label="全部"></el-option>
          <el-option v-for="item in statusType" :key="item.status" :label="item.title" :value="item.status" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" type="flex" justify="space-between">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:dict:add']">
          新增
        </el-button>

      </el-col>
      <el-col :span="1.5">
        <el-dropdown trigger="click">
          <el-button type="primary" size="mini">
            导出<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="handleExport('now')">导出当前</el-dropdown-item>
            <el-dropdown-item @click.native="handleExport('all')">导出全部</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-col>
    </el-row>
    <el-table :row-class-name="rowStyle" border v-loading="loading" highlight-current-row :data="technologyList"
      @selection-change="handleSelectionChange" height="calc(100vh - 250px)" :header-cell-style="{
        background: '#ebebeb'
      }">
      <el-table-column type="selection" width="55" align="center" />
      <af-table-column label="序号" align="center" type="index" />
      <af-table-column label="字段" align="center" prop="fieldName" show-overflow-tooltip />
      <af-table-column label="字段内容" align="center" prop="fieldContent" show-overflow-tooltip />
      <af-table-column label="技术环节" align="center" show-overflow-tooltip >
        <template slot-scope="scope">
          {{ fieldCategoryName[scope.row.fieldCategory] }}
        </template>
      </af-table-column>

      <af-table-column label="质控范围" align="center" show-overflow-tooltip >
        <template slot-scope="scope">
          {{ scope.row.minScope }}-{{ scope.row.maxScope }}
        </template>
      </af-table-column>

      <af-table-column label="微生物是否质控" width="120" align="center" prop="status" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isMicroorganismControl == 1" active-color="#169bd5" inactive-color="#aaaaaa"
            @change="switchShowMicroorganism(scope.row)" />
        </template>
      </af-table-column>
      <af-table-column label="肿瘤是否质控" align="center" prop="status" width="120">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isTumorControl == 1" active-color="#169bd5" inactive-color="#aaaaaa"
            @change="switchShowTumor(scope.row)" />
        </template>
      </af-table-column>
      <af-table-column label="科研是否质控" align="center" prop="status"  width="120">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isResearchControl == 1" active-color="#169bd5" inactive-color="#aaaaaa"
            @change="switchShowResearch(scope.row)" />
        </template>
      </af-table-column>

      <af-table-column label="状态" align="center" prop="status" width="120" >
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status == 1" active-color="#169bd5" inactive-color="#aaaaaa"
            @change="switchShowStatus(scope.row, '有没有')" />
        </template>
      </af-table-column>


      <el-table-column label="操作"  align="center" width="150" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dict:edit']">编辑</el-button>
          <el-button v-if="scope.row.status === 0" size="mini" style="color: #FA5555;" type="text" icon="el-icon-delete"
            @click="handleDelete(scope.row)" v-hasPermi="['system:dict:remove']">删除</el-button>
          <el-button v-else size="mini" style="color: #999999;" type="text" icon="el-icon-delete" disabled>删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div>
      <pagination   style="margin:0;padding: 0;" :total="total" :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize" @pagination="getList" />
    </div>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="技术环节" prop="fieldCategory">
          <el-select v-model="form.fieldCategory" placeholder="请选择技术环节" clearable size="small" style="width: 100%">
            <el-option v-for="item in fieldCategoryType" :key="item.type" :label="item.title" :value="item.type" />
          </el-select>
        </el-form-item>
        <el-form-item label="字段" prop="fieldName">
          <el-input v-model="form.fieldName" placeholder="请输入字段" />
        </el-form-item>
        <el-form-item label="字段内容" prop="fieldContent">
          <el-input v-model="form.fieldContent" placeholder="请输入字段内容" />
        </el-form-item>
        <el-form-item label="质控范围" :required="true" :show-message="false">
          <el-row>
            <el-col :span="11">
              <el-form-item label-width="0" label="" prop="minScope" style="margin-bottom: 0;">
                <el-input placeholder="请输入最小质控范围" v-model="form.minScope" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="2" align="center">-</el-col>
            <el-col :span="11">
              <el-form-item label-width="0" label="" prop="maxScope" style="margin-bottom: 0;">
                <el-input placeholder="请输入最大质控范围" v-model="form.maxScope" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { technology, technologyUpdate, technologyEdit, technologyAdd, technologyDel, technologyExport } from "@/api/dictionary/qualityControl/technology"
import { solveDictionary } from '@/api/dictionary/interpretationDictionary/solveDictionary'

export default {
  name: "technology",
  data () {
    var validateMinScope = (rules, value, callback) => {
      if (!value) {
        callback(new Error("请输入最小质控范围"))
      } else if (!value.toString().match(/^(-)?[0-9]+([.][0-9]{1,})?$/)) {
        callback(new Error('填写格式错误'))
      } else {
        callback()
      }
    }
    var validateMaxScope = (rules, value, callback) => {
      if (!value) {
        callback(new Error("请输入最大质控范围"))
      } else if (!value.toString().match(/^(-)?[0-9]+([.][0-9]{1,})?$/)) {
        callback(new Error('填写格式错误'))
      } else {
        callback()
      }
    }
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 字典表格数据
      technologyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        keyName: undefined,
        fieldCategory: undefined,
        status: ''
      },
      fieldCategoryType: [
        {
          title: '提取',
          type: 10
        },
        {
          title: '建库',
          type: 20
        },
        {
          title: '测序',
          type: 30
        }
      ],
      statusType: [
        {
          title: '开启',
          status: 1
        },
        {
          title: '禁用',
          status: 0
        }
      ],
      fieldCategoryName: {
        10: '提取',
        20: '建库',
        30: '测序'
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fieldCategory: [
          { required: true, message: "请选择技术环节", trigger: ["blur", "change"] }
        ],
        fieldName: [
          { required: true, message: "请输入字段", trigger: "blur" }
        ],
        fieldContent: [
          { required: true, message: "请输入字段内容", trigger: "blur" }
        ],
        minScope: [
          { required: true, validator: validateMinScope, trigger: "blur" }
        ],
        maxScope: [
          { required: true, validator: validateMaxScope, trigger: "blur" }
        ]
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    /** 查询微生物列表 */
    getList () {
      this.loading = true
      technology(this.queryParams).then(response => {
        this.technologyList = response.rows
        this.total = response.total
        this.loading = false
      }
      )
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.form = {
        keyName: undefined,
        fieldCategory: undefined,
        status: undefined
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.dateRange = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset()
      this.open = true
      this.title = "微生物解读字典：新增"
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset()
      const id = row.id || this.ids
      technologyEdit(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "微生物解读字典：编辑"
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            if (this.form.minScope > this.form.maxScope) {
              this.msgError("最大值不可比最小值小")
              return false
            }
            technologyUpdate(this.form).then(response => {
              this.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            this.form.status = 0
            if (this.form.minScope > this.form.maxScope) {
              this.msgError("最大值不可比最小值小")
              return false
            }
            technologyAdd(this.form).then(response => {
              this.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const id = row.id || this.ids
      this.$confirm('是否确认删除选中的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return technologyDel(id)
      }).then(() => {
        this.getList()
        this.msgSuccess("删除成功")
      })
    },
    // /** 导出按钮操作 */
    handleExport (e) {
      var different = ''
      var params = ''
      if (e === 'now') {
        different = '当前'
        params = this.queryParams //对应接口参数
      } else if (e === 'all') {
        different = '所有'
        params = null
      } else {
        return
      }
      let that = this
      let loading
      this.$confirm('是否确认导出' + different + '数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        loading = that.$loading({
          lock: true,
          text: '导出中',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
        technology(params).then((response) => {
          let mydata = [
            [
              '序号',
              '字段',
              '字段内容',
              '技术环节',
              '质控范围',
              '微生物是否质控',
              '肿瘤是否质控',
              '科研是否质控',
              '状态',
            ],
            ...response.rows.map((item, index) => ([
              index + 1,
              item.fieldName,
              item.fieldContent,
              that.fieldCategoryName[item.fieldCategory],
              item.minScope + '-' + item.maxScope,
              item.isMicroorganismControl === 0 ? '禁用' : '开启',
              item.isTumorControl === 0 ? '禁用' : '开启',
              item.isResearchControl === 0 ? '禁用' : '开启',
              item.status === 0 ? '禁用' : '开启',
            ]))
          ]
          let requireMerge = []
          that.$util.createxlsx({
            mydata,
            fileName: '技术字段字典',
            dataList: response.rows,
            type: 'orderId',
            requireMerge,
            loading
          })
        })
      }).catch(function () { })
    },
    //切换状态
    switchShowStatus (row) {
      if (row.status === 1) {
        this.handleUnPublish(row)
      } else if (row.status === 0) {
        this.handlePublish(row)
      }
    },
    handlePublish (row) {
      this.$confirm('是否开启', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return technologyUpdate({
          ...row,
          status: 1
        })
      }).then(() => {
        this.getList()
        this.msgSuccess("开启成功")
      }).catch(function () {
      })
    },

    handleUnPublish (row) {
      this.$confirm('是否禁用', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return technologyUpdate({
          ...row,
          status: 0
        })
      }).then(() => {
        this.getList()
        this.msgSuccess("禁用成功")
      }).catch(function () {
      })
    },

    //切换微生物是否质控
    switchShowMicroorganism (row) {
      if (row.isMicroorganismControl === 1) {
        this.handleUnMicroorganism(row)
      } else if (row.isMicroorganismControl === 0) {
        this.handleMicroorganism(row)
      }
    },
    handleMicroorganism (row) {
      this.$confirm('是否开启', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return technologyUpdate({
          ...row,
          isMicroorganismControl: 1
        })
      }).then(() => {
        this.getList()
        this.msgSuccess("开启成功")
      }).catch(function () {
      })
    },

    handleUnMicroorganism (row) {
      this.$confirm('是否禁用', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return technologyUpdate({
          ...row,
          isMicroorganismControl: 0
        })
      }).then(() => {
        this.getList()
        this.msgSuccess("禁用成功")
      }).catch(function () {
      })
    },


    //切换科研是否质控
    switchShowResearch (row) {
      if (row.isResearchControl === 1) {
        this.handleUnResearch(row)
      } else if (row.isResearchControl === 0) {
        this.handleResearch(row)
      }
    },
    handleResearch (row) {
      this.$confirm('是否开启', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return technologyUpdate({
          ...row,
          isResearchControl: 1
        })
      }).then(() => {
        this.getList()
        this.msgSuccess("开启成功")
      }).catch(function () {
      })
    },

    handleUnResearch (row) {
      this.$confirm('是否禁用', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return technologyUpdate({
          ...row,
          isResearchControl: 0
        })
      }).then(() => {
        this.getList()
        this.msgSuccess("禁用成功")
      }).catch(function () {
      })
    },


    //切换肿瘤是否质控
    switchShowTumor (row) {
      if (row.isTumorControl === 1) {
        this.handleUnTumor(row)
      } else if (row.isTumorControl === 0) {
        this.handleTumor(row)
      }
    },
    handleTumor (row) {
      this.$confirm('是否开启', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return technologyUpdate({
          ...row,
          isTumorControl: 1
        })
      }).then(() => {
        this.getList()
        this.msgSuccess("开启成功")
      }).catch(function () {
      })
    },

    handleUnTumor (row) {
      this.$confirm('是否禁用', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return technologyUpdate({
          ...row,
          isTumorControl: 0
        })
      }).then(() => {
        this.getList()
        this.msgSuccess("禁用成功")
      }).catch(function () {
      })
    },
    rowStyle ({ row }) {
      var arr = this.ids
      for (let i = 0; i < arr.length; i++) {
        if (row.id === arr[i]) {
          return "rowStyle"
        }
      }
    },

  }
}
</script>
<style scoped>
.app-container {
  height: 85vh;
}

::v-deep .el-table__body tr.current-row>td {

  background: #a8d6fe !important;
}

::v-deep .rowStyle {
  background-color: #a8d6fe !important;
}

::v-deep .el-table__body tr:hover>td {
  background: #a8d6fe !important;
}
</style>
